<template>
  <el-form :model="form" label-width="auto" style="max-width: 600px">
    <el-form-item label="助教姓名">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="助教工号/学号">
      <el-input v-model="form.id" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="addAssist">添加助教</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData" table-layout="fixed">
    <el-table-column label="ID" width="480">
      <template #default="scope">
        <span style="margin-left: 10px">{{ scope.row.teacherID }}</span>
      </template>
    </el-table-column>
    <el-table-column label="姓名" width="480">
      <template #default="scope">
        <span style="margin-left: 10px">{{ scope.row.name }}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" type="danger" @click="handleDelete(scope.row.teacherID, scope.row.name)">Delete</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
const form = ref({
  name: '',
  id: ''
})
import { Timer } from '@element-plus/icons-vue'
import { deleteAssistService } from '@/api/teacherApi/deleteAssistService.js'
import { useClassIDStore } from '@/stores/classID.js'
import { ref } from 'vue'
const classIDStore = useClassIDStore();
const handleDelete = async (teacherID, name) => {
  let classID = classIDStore.classID;
  let result = await deleteAssistService(classID,teacherID);
  if (result.code == 156) {
        ElMessage({
            message: '删除成功',
            type: 'success',
        })
    }
    else {
        ElMessage.error('删除失败')
    }
}

import { addAssistService } from '@/api/teacherApi/addAssistService.js'
import { ElMessage } from 'element-plus'
const addAssist = async () => {
  let classID = classIDStore.classID;
  let result = await addAssistService(classID,form.value.id,form.value.name);
  console.log(result)
  if (result.code == 200) {
        ElMessage({
            message: '添加成功',
            type: 'success',
        })
    }
    else {
        ElMessage.error('添加失败')
    }
}
import { getAssistInfoService } from '@/api/teacherApi/getAssistInfoService.js'
const getAssistInfo = async () => {
  let classID = classIDStore.classID;
  let result = await getAssistInfoService(classID);
  console.log(result)
  tableData.value = result
}
getAssistInfo()
const tableData = ref()
</script>
